import UtilFuncs from '../../common/tools/utilfuncs.js'
var utilfuncs = UtilFuncs.UtilFuncs;

import LXCOM from '../../common/common.js'

var AbsorbRectPos = {}

AbsorbRectPos.calculate = function (mousePt, destPos) {
    let x = mousePt.x;
    let y = mousePt.y;

    //求当前鼠标点离矩形每个边最近的吸附点以及距离
    var tmpPoint1 = { x, y };
    var dis1 = this.pt2LineSecDis(destPos.left, destPos.top, destPos.right, destPos.top, tmpPoint1, mousePt);

      x = mousePt.x;
      y = mousePt.y;
    var tmpPoint2 = { x, y };
    var dis2 = this.pt2LineSecDis(destPos.right, destPos.top, destPos.right, destPos.bottom, tmpPoint2, mousePt);

      x = mousePt.x;
      y = mousePt.y;
    var tmpPoint3 = { x, y };
    var dis3 = this.pt2LineSecDis(destPos.right, destPos.bottom, destPos.left, destPos.bottom, tmpPoint3, mousePt);

      x = mousePt.x;
      y = mousePt.y;
    var tmpPoint4 = { x, y };
    var dis4 = this.pt2LineSecDis(destPos.left, destPos.top, destPos.left, destPos.bottom, tmpPoint4, mousePt);

    var mindis = -1;
    var absobPt = { x, y };


    if (dis1 > 0) {
        mindis = dis1;
        absobPt.x = tmpPoint1.x;
        absobPt.y = tmpPoint1.y;
    }

    if (dis2 > 0) {
        if (mindis > dis2 || mindis < 0) {
            mindis = dis2;
            absobPt.x = tmpPoint2.x;
            absobPt.y = tmpPoint2.y;
        }
    }

    if (dis3 >= 0) {
        if (mindis > dis3 || mindis < 0) {
            mindis = dis3;
            absobPt.x = tmpPoint3.x;
            absobPt.y = tmpPoint3.y;
        }
    }

    if (dis4 >= 0) {
        if (mindis > dis4 || mindis < 0) {
            mindis = dis4;
            absobPt.x = tmpPoint4.x;
            absobPt.y = tmpPoint4.y;
        }
    }
    return absobPt;
}

AbsorbRectPos.pt2LineSecDis = function (x1, y1, x2, y2, absobPt, mousePt) {
    let x = mousePt.x;
    let y = mousePt.y;
    var tmpPoint = { x, y };
      x = x1;
      y = y1;
    var pt1 = { x, y };

      x = x2;
      y = y2;
    var pt2 = { x, y };

    var linesec = new LXCOM.LGXLineSection();
    linesec.setPt(pt1, pt2);
    var dis = utilfuncs.pt2LineSec(mousePt, linesec, tmpPoint);
    absobPt.x = tmpPoint.x;
    absobPt.y = tmpPoint.y;
    return dis;
}

export default { AbsorbRectPos }